import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

// 导入页面组件
import Home from '../view/home/Home.vue'
import UserInfo from '../view/user-info/UserInfo.vue'
import UserDetail from '../view/user-detail/UserDetail.vue'

const OAuthTest = () => import('../view/user/OAuthTest.vue')

// 企业相关页面
const CompanyBound = () => import('../view/company/company-bound/CompanyBound.vue')
const CompanyInfo = () => import('../view/company/company-info/CompanyInfo.vue')
const CompanyAdd = () => import('../view/company/company-add/CompanyAdd.vue')
const CompanyUnbound = () => import('../view/company/company-unbound/CompanyUnbound.vue')
// 通知列表页面
const NoticeList = () => import('../view/notice-list/NoticeList.vue')
// 通知详情页面
const NoticeDetailPage = () => import('../view/notice-list/NoticeDetailPage.vue')
// 设施点检入口页面
const FacilityCheckEnter = () => import('../view/form-manager/facility-check/enter/index.vue')
// 废气治理设施点检页面
const AirFacilityCheck = () => import('../view/form-manager/facility-check/air/index.vue')
// 废水治理设施点检页面
const WaterFacilityCheck = () => import('../view/form-manager/facility-check/water/index.vue')
// 固危废贮存场所点检页面
const HhwFacilityCheck = () => import('../view/form-manager/facility-check/hhw/index.vue')
// 中水回用设施点检页面
const GraywaterFacilityCheck = () => import('../view/form-manager/facility-check/graywater/index.vue')
// 上报事项页面
const ReportPage = () => import('../view/report/Report.vue')
// 问题整改 列表页面
const ProblemListPage = () => import('../view/form-manager/problem-list/ProblemList.vue')
// 法律法规列表页面
const RegulationList = () => import('../view/regulation/RegulationList.vue')
// 法律法规详情页面
const RegulationDetail = () => import('../view/regulation/RegulationDetail.vue')

const routes = [
	{
		path: '/',
		name: 'Home',
		component: Home,
		meta: {
			title: '生态昆山政企通',
			keepAlive: false,
		},
	},
	{
		path: '/user',
		name: 'User',
		component: UserInfo,
		meta: {
			title: '个人中心',
			requireAuth: false, // 暂时设为false便于测试
		},
	},

	{
		path: '/user-detail',
		name: 'UserDetail',
		component: UserDetail,
		meta: {
			title: '个人信息',
			requireAuth: false,
		},
	},

	{
		path: '/oauth-test',
		name: 'OAuthTest',
		component: OAuthTest,
		meta: {
			title: 'OAuth测试',
			requireAuth: false,
		},
	},
	// 企业相关路由
	{
		path: '/company/bound',
		name: 'CompanyBound',
		component: CompanyBound,
		meta: {
			title: '企业绑定',
			requireAuth: false,
		},
	},
	{
		path: '/company/info',
		name: 'CompanyInfo',
		component: CompanyInfo,
		meta: {
			title: '企业信息',
			requireAuth: true,
		},
	},
	{
		path: '/company/add',
		name: 'CompanyAdd',
		component: CompanyAdd,
		meta: {
			title: '新增企业',
			requireAuth: false,
		},
	},
	{
		path: '/company/unbound',
		name: 'CompanyUnbound',
		component: CompanyUnbound,
		meta: {
			title: '企业解绑',
			requireAuth: true,
		},
	},
	{
		path: '/notice/list',
		name: 'NoticeList',
		component: NoticeList,
		meta: {
			title: '通知列表',
			keepAlive: false,
		},
	},
	{
		path: '/notice/detail',
		name: 'NoticeDetail',
		component: NoticeDetailPage,
		meta: {
			title: '通知详情',
			keepAlive: false,
		},
	},
	{
		path: '/data/summary',
		name: 'DataSummary',
		component: () => import('@/view/data-summary/DataSummary.vue'),
		meta: {
			title: '数据汇总',
			keepAlive: false,
		},
	},
	// 设施点检入口
	{
		path: '/facility/check',
		name: 'FacilityCheckEnter',
		component: FacilityCheckEnter,
		meta: {
			title: '设施点检',
			keepAlive: false,
		},
	},
	// 废气治理设施点检
	{
		path: '/form-manager/facility-check/air',
		name: 'AirFacilityCheck',
		component: AirFacilityCheck,
		meta: {
			title: '废气治理设施点检',
			keepAlive: false,
		},
	},
	// 废水治理设施点检
	{
		path: '/form-manager/facility-check/water',
		name: 'WaterFacilityCheck',
		component: WaterFacilityCheck,
		meta: {
			title: '废水治理设施点检',
			keepAlive: false,
		},
	},
	// 固危废贮存场所点检
	{
		path: '/form-manager/facility-check/hhw',
		name: 'HhwFacilityCheck',
		component: HhwFacilityCheck,
		meta: {
			title: '固危废贮存场所点检',
			keepAlive: false,
		},
	},
	// 中水回用设施点检
	{
		path: '/form-manager/facility-check/graywater',
		name: 'GraywaterFacilityCheck',
		component: GraywaterFacilityCheck,
		meta: {
			title: '中水回用设施点检',
			keepAlive: false,
		},
	},
	// 上报事项（企业端）
	{
		path: '/report',
		name: 'Report',
		component: ReportPage,
		meta: {
			title: '上报事项',
			keepAlive: false,
		},
	},
	// 问题整改 列表
	{
		path: '/problem/list',
		name: 'ProblemList',
		component: ProblemListPage,
		meta: {
			title: '问题整改',
			keepAlive: false,
		},
	},
	// 问题整改 详情
	{
		path: '/form-manager/problem',
		name: 'Problem',
		component: () => import('@/view/form-manager/problem/Problem.vue'),
		meta: {
			title: '问题整改-表单',
			keepAlive: false,
		},
	},
	// 法律法规列表（公开访问，不需要登录）
	{
		path: '/regulation/list',
		name: 'RegulationList',
		component: RegulationList,
		meta: {
			title: '法律法规',
			keepAlive: false,
			requireAuth: false, // 公开访问，游客可访问
			isPublic: true, // 标记为公开页面
		},
	},
	// 法律法规详情（公开访问，不需要登录）
	{
		path: '/regulation/detail',
		name: 'RegulationDetail',
		component: RegulationDetail,
		meta: {
			title: '法规详情',
			keepAlive: false,
			requireAuth: false, // 公开访问，游客可访问
			isPublic: true, // 标记为公开页面
		},
	},
]

const router = createRouter({
	history: createWebHistory(import.meta.env.MODE === 'development' ? '/' : import.meta.env.VITE_BASE_PATH || '/'),
	routes,
})

// 路由守卫
router.beforeEach((to, from, next) => {
	document.title = to.meta.title || '政企通'
	const userStore = useUserStore()
	// 公开页面直接通过
	if (to.meta.isPublic === true || to.meta.requireAuth === false) {
		return next()
	}
	// 需要鉴权的页面：未登录则回到首页触发授权流程
	if (to.meta.requireAuth && !userStore.isAuthenticated) {
		return next({ path: '/' })
	}
	next()
})

export default router
